package com.cchc.k8s.config;

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStreamReader;


/**
 * file：KubernetesConfig.java
 * description:k8s连接配置
 * date: 2021-09-16 15:04
 * author: wenrui
 * version: 1.0
 */
@Component
public class KubernetesConfig {

    @Autowired
    ResourceLoader resourceLoader;
    @Value("${kubernetes.config-file}")
    String configFile;

    @Bean
    public ApiClient k8sClient() throws IOException {
        ApiClient client = ClientBuilder.kubeconfig(
                KubeConfig.loadKubeConfig(
                        new InputStreamReader(resourceLoader.getResource(configFile).getInputStream())
                )
        ).build();
        Configuration.setDefaultApiClient(client);
        return client;
    }
}
